home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10126 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  54 lines

  1. Path: iz.maus.de!Torsten_Landschoff
  2. From: Torsten_Landschoff@iz.maus.de (Torsten Landschoff)
  3. Newsgroups: comp.lang.c++
  4. Subject: character strings read-in problem!
  5. Message-ID: <199603040716.a35477@iz.maus.de>
  6. Date: Mon, 04 Mar 96 05:16:00 GMT
  7. References: <4h2mkr$5au@sunburst.ccs.yorku.ca>
  8. X-Gate: MausGate/News 1.25/ac3
  9. MIME-Version: 1.0
  10. Content-Type: text/plain; charset=ISO-8859-1
  11. Content-Transfer-Encoding: 8bit
  12.  
  13. -A13634@AC3
  14.  
  15. Hi KuMan
  16.  
  17. K>I'm trying to read in strings from a file and put them in an array of
  18. K>26 different cells according to their order alphabetically.
  19.  
  20. K>#include <stdio.h>
  21. K>#include <stdlib.h>
  22.  
  23. K>void main(){
  24.  
  25. better: int     main( void ) {
  26.  
  27. K>FILE  *input;
  28. K>char  name[20];
  29. K>char  *stop_word[26];
  30.  
  31. K>input = fopen("QQ.txt", "r");
  32. K>while(!feof(input)){
  33. K>        fscanf(input, "%s", name);
  34. K>        stop_word[((name[0] >= 97)? name[0]-96 : name[0]%64)] = name;
  35.  
  36. Here is the problem: You can't assign a character string as any ordinary type -
  37. you must call a special function to do that. After this assignment every assign
  38. entry of stop_word will contain a pointer to name, which has the value of the
  39. last string read. 
  40.  
  41. To put this right, you could replace the original line by the following:
  42.  
  43.     stop_word[((name[0] >= 97) ? name[0]-96 : name[0]%64)] =     strdup(name);
  44.     
  45. K>}
  46. printf("%s \n", stop_word[1]);
  47. printf("%s \n", stop_word[3]);
  48. }
  49. fclose(input);
  50. }
  51.  
  52. cu
  53.     Torsten
  54.